<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class ArticleController extends Controller
{
    // 中间键 判断是否登录
    public function __construct()
    {
        $this -> middleware('login');
    }
    // index方法,显示文章列表
    public function index(Request $request)
    {

        $num = $request -> input('num', 5);
        $keywords = $request -> input('keywords','');
        // dd($userData);
        $data = DB::table('categoryinfo')
            -> select('categoryinfo.*')
            -> leftJoin('category', 'categoryinfo.aid', '=', 'category.id')
            -> where('categoryinfo.title', 'like', '%'.$keywords.'%')
            // -> orderBy('sortpath')
            -> paginate($num);
            // dd($data);
        return view('admin.article.index', ['data' => $data, 'request' => $request -> all(), 'title' => '文章列表']);
    }

    // 添加文章add方法
    public function add(Request $request)
    {
        // dd($userData);
        $data = DB::table('category')
            -> select('category.*')
            -> get();
        // dd($data);
        // dd($data);
        foreach ($data as $key => $value) {
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$key] -> catname = $str.$value -> catname;
        }
        return view('admin.article.add', ['data' => $data, 'title' => '添加文章']);
    }

    // // 处理添加过来的文章doadd()方法
    public function doadd(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        $time = date('Y-m-d H:i:s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        $res = DB::table('categoryinfo') -> insert($data);
        if($res)
        {
            return redirect('/Admin/article/index') -> with(['info' => '发表成功']);
        }else
        {
            return back() -> with(['info' => '发表失败']);
        }
    }

    // 文章详情页 查看发表的分类等 details()
    public function details($aid)
    {
        $data = DB::table('categoryinfo')
            // -> select('categoryinfo.*')
            -> leftJoin('category', 'categoryinfo.aid', '=', 'category.id')
            -> where('category.id', '=', $aid)
            -> get();
        // dd($data);
        return view('admin.article.details', ['data' => $data, 'title' => '文章列表']);
    }

    // 修改文章内容 edit()方法
    public function edit($id)
    {
        $data = DB::table('categoryinfo')
            // -> select('category.catname')
            -> leftJoin('category', 'categoryinfo.aid', '=', 'category.id')
            -> where('categoryinfo.cid', '=', $id)
            -> get();
        // dd($data);
        return view('admin.article.edit',['data' => $data, 'title' => '修改文章']);
    }

    // 修改文章,提交过来的数据 update()
    public function update(Request $request)
    {
        // return 11111;
        // $data = $request -> except('_token','aid');
        $data = $request -> all();
        dd($data);
        $res = DB::table('category') -> where('id',$data['pid']) -> update($data);
    }

    // 删除文章delete()
    public function delete($cid)
    {
        $res = DB::table('categoryinfo') -> where('cid',$cid) -> delete();

        if($res)
        {
            return redirect('/Admin/article/index') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    }

    // 批量删除文章,pidelete()
    public function pidelete(Request $request)
    {
        $data = DB::table('categoryinfo')
            // -> select('category.catname')
            -> leftJoin('category', 'categoryinfo.aid', '=', 'category.id')
            // -> where('categoryinfo.cid', '=', $id)
            -> get();
        return view('admin.article.pidelete', ['data' => $data, 'title' => '批量删除文章']);
    }

    // 批量删除文章选中的dodelete()
    public function dodelete(Request $request)
    {
        $data = $request -> except('_token','num','keywords');
        // 处理提交过来的数组 拆分成字符串.其实并不用
        // dd($data);
        // $str = '';
        // foreach ($data as $v) {
        //     for ($i=0; $i <count($v); $i++) {
        //         $str .= $v[$i].',';
        //     }
        // }
        // $str = rtrim($str,',');
        // dd($str);
        $res = DB::table('categoryinfo') -> whereIn('cid', $data['cid']) -> delete();
        if($res)
        {
            return redirect('/Admin/article/index') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    }
}
